package cn.play.cardgame.util;

import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;

import android.content.Context;
import android.os.Bundle;

import java.util.HashMap;
import java.util.Map;

public class SoundUtils {


    private static String[][] pai_voice = {
            {"黑桃1", "黑桃2", "黑桃3", "黑桃4", "黑桃5", "黑桃6", "黑桃7", "黑桃8", "黑桃9", "黑桃10", "黑桃勾", "黑桃圈", "黑桃K"},
            {"红桃1", "红桃2", "红桃3", "红桃4", "红桃5", "红桃6", "红桃7", "红桃8", "红桃9", "红桃10", "红桃勾", "红桃圈", "红桃K"},
            {"草花1", "草花2", "草花3", "草花4", "草花5", "草花6", "草花7", "草花8", "草花9", "草花10", "草花勾", "草花圈", "草花K"},
            {"方块1", "方块2", "方块3", "方块4", "方块5", "方块6", "方块7", "方块8", "方块9", "方块10", "方块勾", "方块圈", "方块K"}};

    private SoundUtils() {
    }

    private static SoundUtils soundUtils;

    public static SoundUtils getInstance() {
        if (soundUtils == null)
            soundUtils = new SoundUtils();
        return soundUtils;
    }

    public void generateAudio(final Context context, final String text) {

        LogUtil.getLogger().d7("08221058", "huase = " + text);

        new Thread(new Runnable() {

            @Override
            public void run() {
                // 1.创建 SpeechSynthesizer 对象, 第二个参数:本地合成时传 InitListener
                SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(context, null);
                // 2.合成参数设置,详见《MSC Reference Manual》SpeechSynthesizer 类
                // //设置发音人(更多在线发音人,用户可参见 附录13.2
                mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan"); // 设置发音人
                mTts.setParameter(SpeechConstant.SPEED, "50");// 设置语速
                mTts.setParameter(SpeechConstant.VOLUME, "80");// 设置音量,范围 0~100
                mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); // 设置云端
                // 设置合成音频保存位置(可自定义保存位置),保存在“./sdcard/iflytek.pcm”
                // 保存在 SD 卡需要在 AndroidManifest.xml 添加写 SD 卡权限
                // 仅支持保存为 pcm 和 wav 格式,如果不需要保存合成音频,注释该行代码
                // mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH,
                // "./sdcard/iflytek.pcm");
                //3.开始合成
                mTts.startSpeaking(text, mSynListener);
            }
        }).start();

    }

    // 合成监听器
    private SynthesizerListener mSynListener = new SynthesizerListener() {
        // 会话结束回调接口,没有错误时,error为null
        public void onCompleted(SpeechError error) {
        }

        // 缓冲进度回调
        // //percent为缓冲进度0~100,beginPos为缓冲音频在文本中开始位置,endPos表示缓冲音频在文本中结束位置,info为附加信息。
        public void onBufferProgress(int percent, int beginPos, int endPos, String info) {
        }

        // 开始播放
        public void onSpeakBegin() {
        }

        // 暂停播放
        public void onSpeakPaused() {
        }

        // 播放进度回调
        // //percent为播放进度0~100,beginPos为播放音频在文本中开始位置,endPos表示播放音频在文本中结束位置.
        public void onSpeakProgress(int percent, int beginPos, int endPos) {
        } // 恢复播放回调接口

        public void onSpeakResumed() {
        }

        // 会话事件回调接口
        public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
        }
    };

    public String generateVoiceText(String huase, int index) {
        if (huase.equals("Heixin")) {
            return pai_voice[0][index];
        } else if (huase.equals("Hongxin")) {
            return pai_voice[1][index];
        } else if (huase.equals("Caohua")) {
            return pai_voice[2][index];
        } else if (huase.equals("Fangkuai")) {
            return pai_voice[3][index];
        } else if (huase.equals("a") || huase.equals("b")) {
            return "王";
        }
        return huase;
    }

}
